﻿using System;
using System.Collections.Generic;
using System.Linq;
using Keys = System.Windows.Forms.Keys;
using RoMFramework.RomAPI;
using System.Windows.Controls;
using System.Xml.Linq;

namespace RoMFramework.Hacks
{
    
    public abstract class HackBase
    {
        
        protected RomClient m_Client;
        public List<Keys> CurrentHotKeys;
        public HackUsage Usage;
        public bool Enabled;
        public string HackName;
        public Label LblHackStatus, LblHackName;
        public TextBox TxtBHackHotkey, TxtBValue;
        public Button ButtonToggle;
        public abstract List<Keys> HotKeys
        { 
            get;
            protected set;
        }
        public abstract List<Keys> DefaultHotKeys
        {
            get;
            protected set;
        }

        protected HackBase(RomClient client)
        {
            m_Client = client;
        }

        /// <summary>
        /// Enable the Hack
        /// </summary>
        public abstract void Enable(Keys keys);

        /// <summary>
        /// Disable the Hack
        /// </summary>
        public abstract void Disable(Keys keys);

        /// <summary>
        /// Toggle the Hack
        /// </summary>
        public abstract void Toggle(Keys keys);

        public void LoadHotKeys()
        {
            if (String.IsNullOrEmpty(HackName) || HackName.Trim().Length == 0) return;
            var xml = XElement.Load("Hotkeys.xml");
            var keys = from h in xml.Descendants(HackName) select h;

            foreach (var key in keys)
            {
                if (key.Parent == null) return;
                string value;
                if (key.Parent.Name == "DefaultHotkeys")
                {
                    value = key.Value;
                    try
                    {
                        var keys1 = (Keys)Enum.Parse(typeof(Keys), value);
                        DefaultHotKeys.Add(keys1);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine(string.Format("Error parsing Hack Hotkey {0}", HackName));
                    }
                }
                else if (key.Parent.Name == "Hotkeys")
                {
                    value = key.Value;
                    try
                    {
                        var keys1 = (Keys)Enum.Parse(typeof(Keys), value);
                        HotKeys.Add(keys1);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine(string.Format("Error parsing Hack Hotkey {0}", HackName));
                    } 
                }
            }
        }

    }

    public enum HackUsage
    {
        Toggle,
        Value
    }
}
